/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pckSQL;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;

/**
 *
 * @author Portable-Maison
 */
public class SQL_Connection {

    Connection gaConnection;
    Vector gaListeProgramme = new Vector();

    public SQL_Connection()
    { }
    
    public SQL_Connection (String psDB_Name, String psDB_Adresse, String psDB_User, String psDB_Password)
    {
        try
        {
                Class.forName("com.mysql.jdbc.Driver");
                String connectionUrl = ("jdbc:mysql://" + psDB_Adresse + "/" +
                                       psDB_Name + "?user=" + psDB_User + "&password=" +
                                       psDB_Password);
                gaConnection = DriverManager.getConnection(connectionUrl);
                System.out.println("Connection réussi");
            }
            catch (SQLException e)
            {
                System.out.println("SQL Exception: "+ e.toString());
            }
            catch (ClassNotFoundException cE)
            {
                System.out.println("Class Not Found Exception: "+ cE.toString());
            }
    }

    public static boolean testerConnection(String psDB_Name, String psDB_Adresse, String psDB_User, String psDB_Password)
    {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = ("jdbc:mysql://" + psDB_Adresse + "/" +
                               psDB_Name + "?user=" + psDB_User + "&password=" +
                               psDB_Password);
        Connection laConnection = DriverManager.getConnection(connectionUrl);
        System.out.println("Test de Connection réussi");
        return true;
    }
    catch (SQLException e)
    {
        System.out.println("SQL Exception: "+ e.toString());
        return false;
    }
    catch (ClassNotFoundException cE)
    {
        System.out.println("Class Not Found Exception: "+ cE.toString());
        return false;
    }
    }

    public void finalize() throws SQLException
    {
    gaConnection.close ();
    }

    public static void ScannerDriver()
    {
    for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();)
    {
        Driver driver = (Driver)e.nextElement();
        int majorVersion = driver.getMajorVersion();
        int minorVersion = driver.getMinorVersion();
        System.out.println("Driver = "+driver.getClass()+
            " v"+majorVersion+"."+minorVersion);
    }
    }

    public JComboBox chargeListeProgramme(JComboBox paComboBox)
    {
    Statement stmt = null;
    ResultSet rs = null;
    int liI = 0;
    //SQL query command
    String SQL = "SELECT * FROM programme;";
    try
    {
    stmt = gaConnection.createStatement();
    rs = stmt.executeQuery(SQL);
    while (rs.next())
        {
       gaListeProgramme.add (rs.getString ("CodeCours") + " - " + rs.getString ("NomDuProgramme"));
       paComboBox.addItem (gaListeProgramme.elementAt (liI));
       liI++;
        }
    rs.close ();
    }
    catch (SQLException ex)
    {
        Logger.getLogger(SQL_Connection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return paComboBox;
    }

}
